﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Lg.Core.Form
{
    /// <summary>
    /// 除了非空以外的逻辑较验，前题是值不为空，为空则不进行逻辑较验
    /// </summary>
    public class Validate
    {     
        #region 【自定义验证方法】
        /// <summary>
        /// 非空验证
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static void Required(string? val,string errmsg="必填项不可为空！")
        {
            if (val == null || val.Trim().Length == 0)
            {
                throw new Exceptions.LgException(errmsg);
            }
        }

        /// <summary>
        /// 手机号码格式验证
        /// </summary>
        /// <param name="val">验证对像</param>
        /// <returns>bool</returns>
        public static void Phone(string? val,string errmsg="手机号格式不正确！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                bool b = Regex.IsMatch(val, "^1[3-9][0-9]{9}$");
                if (!b)
                {
                    throw new Exceptions.LgException(errmsg);
                }
            }
                
        }

        /// <summary>
        /// 邮箱格式验证
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static void Email(string? val,string errmsg="Email格式不正确！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                bool b = Regex.IsMatch(val, @"^\w+@\w+(\.\w+)+$");
                if (!b)
                {
                    throw new Exceptions.LgException(errmsg);
                }
            }
            
        }
        /// <summary>
        /// 网址格式较验
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static void Link(string? val, string errmsg = "不是有效的网址格式！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                bool b = Regex.IsMatch(val, @"^(http||https|ftp){0,1}(:\/\/)(www\.)?([a-z0-9:?=&]+)");
                if (!b)
                {
                    throw new Exceptions.LgException(errmsg);
                }
            }

        }

        public static void IdCard(string? val,string errmsg="身份证号码格式不正确！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                bool b = Regex.IsMatch(val, @"^[1-9][0-9]{5}([1][9][0-9]{2}|[2][0][0-9]{2})([0][1-9]|[1][0|1|2])([0][1-9]|[1|2][0-9]|[3][0|1])[0-9]{3}([0-9]|[X])$");
                if (b)
                {
                    int[] weight_factor = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
                    // 校验码
                    string[] check_code = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
                    string last = val.Last().ToString();
                    string seventeen = val.Substring(0, 17);
                    // ISO 7064:1983.MOD 11-2
                    // 判断最后一位校验码是否正确
                    char[] arr = seventeen.ToCharArray();
                    int num = 0;
                    for (int i = 0; i < arr.Length; i++)
                    {
                        int i1 = Convert.ToInt32(arr[i].ToString());
                        int i2 = Convert.ToInt32(weight_factor[i]);
                        num = num + i1 * i2;
                    }
                    // 获取余数
                    int resisue = num % 11;
                    string last_no = check_code[resisue];
                    // 返回验证结果，校验码和格式同时正确才算是合法的身份证号码
                    b = last == last_no;
                }
                if (!b)
                {
                    throw new Exceptions.LgException(errmsg);
                }
            }
                
        }

        public static void Number(string? val, List<int?>? range=null, bool allowDecimals=true,string errmsg="数字格式不正确！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                try
                {
                    double dValue = Convert.ToDouble(val);
                    //验证是否支持小数，不支持的话检测到值为小数时则抛异常
                    if (!allowDecimals && val.Contains('.'))
                    {
                        throw new Exceptions.LgException($"{errmsg}[不支持小数！]");
                    }
                    if (range != null)
                    {
                        if (range[0] != null && range[0] > 0)
                        {
                            var flag = Convert.ToDouble(val) >= range[0];
                            if (!flag)
                            {
                                throw new Exceptions.LgException("数字不能小于:" + range[0]);
                            }
                        }
                        if (range[0] != null && range[1] > 0)
                        {
                            var flag = Convert.ToDouble(val) <= range[1];
                            if (!flag)
                            {
                                throw new Exceptions.LgException("数字不能大于:" + range[1]);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Exceptions.LgException($"{errmsg}[{e.Message}]");
                }
            }
                
        }
        public static void Date(string? val,List<string>? range=null,string errmsg="日期格式有误！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                try
                {
                    DateTime date = DateTime.Parse(val);
                    if (range != null)
                    {
                        if (range[0] != "")
                        {

                            var res = date >= DateTime.Parse(range[0]);
                            if (!res)
                            {
                                throw new Exceptions.LgException("日期不能小于:" + range[0]);
                            }
                        }
                        if (range[1] !="")
                        {
                            var res = date <= DateTime.Parse(range[1]);
                            if (!res)
                            {
                                throw new Exceptions.LgException("日期不能大于:" + range[1]);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Exceptions.LgException($"{errmsg}[{e.Message}]");
                }
            }
                
            
        }

        public static void DataRange(string? val, List<DateTime?>? dateRange=null , string errmsg = "日期格式有误！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                try
                {
                    DateTime date = DateTime.Parse(val);
                    if (dateRange != null)
                    {
                        if (dateRange[0] != null)
                        {
                            var res = date >= dateRange[0];
                            if (!res)
                            {
                                throw new Exceptions.LgException("日期不能小于:" + dateRange[0]);
                            }
                        }
                        if (dateRange[1] != null)
                        {
                            var res = date <= dateRange[0];
                            if (!res)
                            {
                                throw new Exceptions.LgException("日期不能大于:" + dateRange[1]);
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw new Exceptions.LgException($"{errmsg}[{e.Message}]");
                }
            }
                

        }
        public static void Strong(string? val,string errmsg="未通过强密码较验！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                bool b = Regex.IsMatch(val, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[\s\S]{8,30}$");
                if (!b)
                {
                    throw new Exceptions.LgException(errmsg);
                }
            }
            
        }

        public static void Length(string? val,List<int>? range,string errmsg="字符长度较验不通过！")
        {
            if (!string.IsNullOrEmpty(val))
            {
                if (range != null)
                {
                    if (range[0] > 0)
                    {
                        if (val.Length < range[0])
                        {
                            throw new Exceptions.LgException(errmsg);
                        }
                    }
                    if(range[1] > 0)
                    {
                        if(val.Length > range[1])
                        {
                            throw new Exceptions.LgException(errmsg);
                        }
                    }
                }
            }
        }
        #endregion

    }
}
